#include "../apilib.h"
#include "../stdlib.h"

void HariMain(void)
{
	int LBA;
	char c, cmdline[30], *p, *filename;
	char buffer[512];
	int i;
	int fh, fsize;
	
	for(i=0; i<sizeof(buffer); i++)
	{
		buffer[i] = 0;
	}

	api_cmdline(cmdline, 30);
	for (p = cmdline; *p > ' '; p++) { }	/*跳过之前的内容，直到遇到空格*/
	for (; *p == ' '; p++) { }	/*跳过空格*/
	
	LBA = atoi(p);
	
	
	for (; *p > ' '; p++) { }	/*跳过之前的内容，直到遇到空格*/
	if(*p == ' ')
	{
		for (; *p == ' '; p++) { }	/*跳过空格*/
		filename = p;
	}
	else
	{
		filename = 0;
	}
	if(filename != 0) // 如果有文件就读取文件内容到buffer
	{
		fh = api_fopen(filename);
		api_putstr0(filename);
		if (fh != 0) {
			fsize = api_fsize(fh, 0);
			if(fsize > 512)
			{
				fsize = 512;
			}
			api_fread(buffer, fsize, fh);
			printf("read %d byte\r\n", fsize);	
			api_putstr0(buffer);
			api_putstr0("\r\n");
		} else {
			api_putstr0("File not found.\n");
		}
	}
		
	if(api_write_hard_disk_0(LBA, buffer)!= 0)
	{
		api_putstr0("write disk err\r\n");
		api_end();
	}
	api_putstr0("\r\n");
	sprintf(buffer, "write %d byte\r\n", i);
	api_putstr0(buffer);
	api_end();
}
